// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <benchmark/benchmark.h>

#include <vector>

#include <nebula/compute/api_scalar.h>
#include <tests/compute/kernels/test_util.h>
#include <nebula/testing/ktest_util.h>
#include <nebula/testing/random.h>
#include <nebula/util/benchmark_util.h>

namespace nebula {
namespace compute {

constexpr auto kSeed = 0x94378165;

template <typename Type>
static void BenchArrayScalar(benchmark::State& state, const std::string& op) {
  RegressionArgs args(state, /*size_is_bytes=*/false);
  auto ty = TypeTraits<Type>::type_singleton();
  auto rand = random::RandomArrayGenerator(kSeed);
  auto array = rand.ArrayOf(ty, args.size, args.null_proportion);
  auto scalar = *rand.ArrayOf(ty, 1, 0)->get_scalar(0);
  for (auto _ : state) {
    ABORT_NOT_OK(call_function(op, {array, Datum(scalar)}).status());
  }
}

template <typename Type>
static void BenchArrayArray(benchmark::State& state, const std::string& op) {
  RegressionArgs args(state, /*size_is_bytes=*/false);
  auto ty = TypeTraits<Type>::type_singleton();
  auto rand = random::RandomArrayGenerator(kSeed);
  auto lhs = rand.ArrayOf(ty, args.size, args.null_proportion);
  auto rhs = rand.ArrayOf(ty, args.size, args.null_proportion);
  for (auto _ : state) {
    ABORT_NOT_OK(call_function(op, {lhs, rhs}).status());
  }
}

static void GreaterArrayArrayInt64(benchmark::State& state) {
  BenchArrayArray<Int64Type>(state, CompareOperatorToFunctionName(GREATER));
}

static void GreaterArrayScalarInt64(benchmark::State& state) {
  BenchArrayScalar<Int64Type>(state, CompareOperatorToFunctionName(GREATER));
}

static void GreaterArrayArrayString(benchmark::State& state) {
  BenchArrayArray<StringType>(state, CompareOperatorToFunctionName(GREATER));
}

static void GreaterArrayScalarString(benchmark::State& state) {
  BenchArrayScalar<StringType>(state, CompareOperatorToFunctionName(GREATER));
}

static void MaxElementWiseArrayArrayInt64(benchmark::State& state) {
  BenchArrayArray<Int64Type>(state, "max_element_wise");
}

static void MaxElementWiseArrayScalarInt64(benchmark::State& state) {
  BenchArrayScalar<Int64Type>(state, "max_element_wise");
}

static void MaxElementWiseArrayArrayString(benchmark::State& state) {
  BenchArrayArray<StringType>(state, "max_element_wise");
}

static void MaxElementWiseArrayScalarString(benchmark::State& state) {
  BenchArrayScalar<StringType>(state, "max_element_wise");
}

BENCHMARK(GreaterArrayArrayInt64)->Apply(RegressionSetArgs);
BENCHMARK(GreaterArrayScalarInt64)->Apply(RegressionSetArgs);

BENCHMARK(GreaterArrayArrayString)->Apply(RegressionSetArgs);
BENCHMARK(GreaterArrayScalarString)->Apply(RegressionSetArgs);

BENCHMARK(MaxElementWiseArrayArrayInt64)->Apply(RegressionSetArgs);
BENCHMARK(MaxElementWiseArrayScalarInt64)->Apply(RegressionSetArgs);

BENCHMARK(MaxElementWiseArrayArrayString)->Apply(RegressionSetArgs);
BENCHMARK(MaxElementWiseArrayScalarString)->Apply(RegressionSetArgs);

}  // namespace compute
}  // namespace nebula
